/***************************************************************************
创建者：华磊
 开始时间：               : 2020.11.29
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   主菜单窗口程序
*                                                                         *
 ***************************************************************************/
#include "homemenuview.h"
#include "ui_homemenuview.h"
#include "communicatedelegate.h"
#include "qmessagebox.h"
#include "weldpropertydialog.h"
#include "weldconditiondialog.h"
#include "weldsystemdialog.h"

#include "restoredialog.h"
#include "restoreandbackupdlg.h"
#include "upgradehmiexedlg.h"
#include "filebackupdialog.h"
#include "filerestoredialog.h"
#include "weavemotionconditiondialog.h"
#include "palletparametersettingview.h"

HomeMenuView::HomeMenuView(int styleId,CommunicateNormalDelegate *communicateNormalIn,GlobalData *globalDataIn,
                           Login *loginDialogIn,MoveOperation *moveOperationIn,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomeMenuView)
{
        loginDialog=loginDialogIn;
        ui->setupUi(this);
        setGeometry(70,128,460,595);
        setContextMenuPolicy(Qt::CustomContextMenu);

        initialDisplayStyle(styleId);

        //子界面-文件备份
        fileBackUpDialog = new FileBackUpDialog(globalDataIn);
        fileBackUpDialog->setParent(this);
        fileBackUpDialog->hide();
        //子界面-文件还原
        fileRestoreDialog = new FileRestoreDialog(globalDataIn);
        fileRestoreDialog->setParent(this);
        fileRestoreDialog->hide();
        //子界面-软件使能
        manualServonDialog = new ManualServonDialog(styleId);
        manualServonDialog->setParent(this);
        manualServonDialog->hide();
        //子界面-零点标定
        setZeroPointDialog = new SetZeroPointDialog(styleId,communicateNormalIn,globalDataIn);
        setZeroPointDialog->setParent(this);
        setZeroPointDialog->hide();
        //子界面-校准
        touchCheckDialog = new TouchCheckDialog;
        //touchCheckDialog->setParent(this);
        touchCheckDialog->hide();
        //子界面-密码修改
        passwordModiyDialog = new PasswordModiyDialog(styleId);
        passwordModiyDialog->setParent(this);
        passwordModiyDialog->hide();
        //子界面-电机监控器
        motorStatusView = new MotorStatusView(styleId);
        motorStatusView->setParent(this);
        motorStatusView->hide();
        //子界面-注册
        registrationDialog = new RegistrationDialog(styleId);
        registrationDialog->setParent(this);
        registrationDialog->hide();
        //子界面-升级还原
        restoreDialog = new RestoreDialog;
        restoreCheckDialog = new RestoreCheckDialog;
        restoreDialog->setParent(this);
        //restoreCheckDialog->setParent(this);//小窗口不需要
        restoreDialog->hide();
        restoreCheckDialog->hide();
        restoreDialog->restoreCheckDialog = restoreCheckDialog;
        //子界面-日志
        deviceLogDialog = new DeviceLogDialog(styleId);
        deviceLogDialog->setParent(this);
        deviceLogDialog->hide();
        //子界面-坐标系
        setcoordinatedialog = new SetCoordinateDialog(styleId);
        coordinate2Dialog = new SetCoordinate2Dialog(styleId);
        setcoordinatedialog->coordinate2Dialog = coordinate2Dialog;
        setcoordinatedialog->setParent(this);
        coordinate2Dialog->setParent(this);
        setcoordinatedialog->hide();
        coordinate2Dialog->hide();
        //子界面-软件版本
        copyRightDialog = new CopyRightDialog(styleId);
        copyRightDialog->setParent(this);
        copyRightDialog->hide();
        //子界面-公司信息
        companyInfoDialog = new CompanyInfoDialog(styleId);
        companyInfoDialog->setParent(this);
        companyInfoDialog->hide();
        //子界面-ip设置
        ipSettingDialog = new IpSettingDialog(styleId);
        ipSettingDialog->setParent(this);
        ipSettingDialog->hide();
        //子页面-启动方式
        startTypeDialog = new StartTypeDialog(styleId,this);
        startTypeDialog->hide();

        //子页面-3D simulation
        vrepConfigView = new VrepConfigView;
        vrepConfigView->setParent(this);
        vrepConfigView->hide();

        //hualei  文件恢复备份
        restoreAndBackDlg=new RestoreAndBackupDlg(styleId,communicateNormalIn);
//        restoreAndBackDlg->setParent(this,Qt::Popup);
        restoreAndBackDlg->setParent(this);
        //restoreAndBackDlg->show();
        restoreAndBackDlg->hide();

        //hmi升级
        upgradeHmiDlg=new UpgradeHmiExeDlg(styleId);
        upgradeHmiDlg->setParent(this);
        upgradeHmiDlg->hide();

        ethercatDevicefigDlg=new EthercatDeviceConfigDlg(styleId);
        ethercatDevicefigDlg->setParent(this);
        ethercatDevicefigDlg->hide();

        debugInfoDlg=new DebugInformationDlg(styleId);
        debugInfoDlg->setParent(this);
        debugInfoDlg->hide();

        //焊接属性
        weldPropertyDialog = new WeldPropertyDialog(styleId,communicateNormalIn);
        weldPropertyDialog->setParent(this);
        weldPropertyDialog->hide();

        //焊接条件
        weldConditionDialog = new WeldConditionDialog(styleId,communicateNormalIn);
        weldConditionDialog->setParent(this);
        weldConditionDialog->hide();

        weaveConditionDialog = new WeaveMotionConditionDialog(styleId,communicateNormalIn);
        weaveConditionDialog->setParent(this);
        weaveConditionDialog->hide();

        //弧焊系统设置
        weldSystemDialog = new WeldSystemDialog(styleId,communicateNormalIn);
        weldSystemDialog->setParent(this);
        weldSystemDialog->hide();

        palletSetDialog=new PalletParameterSettingView(styleId,globalDataIn,moveOperationIn,communicateNormalIn,this);
        palletSetDialog->hide();
        //按钮刷新
        menuShowPoint.setX(175);
        menuShowPoint.setY(0);
        point1.setX(75);
        point1.setY(100);
        point2.setX(75);
        point2.setY(100);
        point3.setX(75);
        point3.setY(250);
        point4.setX(75);
        point4.setY(320);
        point5.setX(75);
        point5.setY(380);
        point6.setX(75);
        point6.setY(420);
        point7.setX(75);
        point7.setY(350);

        //设置菜单样式
//        styleMenu = "QMenu { background-color: white;border: 1px solid black;}";
//        styleMenu = styleMenu + "QMenu::item {background-color: transparent;padding:8px 32px; margin:0px 8px;border-bottom:1px solid #DBDBDB;}";
//        styleMenu = styleMenu + "QMenu::item:selected {background-color: #2dabf9;}";

        // 整个背景颜色  整个菜单边缘
        styleMenu = "QMenu {\
                            background-color: rgb(255, 255, 255);\
                            border: 3px solid rgb(1, 147, 195);\
                     }";

        // 字体样式颜色
        // item边框
        // 设置菜单文字上下和左右的内边距，菜单中上下选项有间隔
        // 设置菜单项的外边距
        styleMenu += "QMenu::item {\
                             font-size: 10pt;\
                             color: rgb(255,255,255);\
                             border: 3px solid rgb(60,60,60);\
                             background-color: rgb(1, 147, 195);\
                             padding:16px 16px;\
                             margin:2px 2px;\
                      }";

        switch (styleId) {
            case 1:
                styleMenu += "QMenu::item {\
                            font-size: 10pt;\
                            color: rgb(255,255,255);\
                            border: 3px solid rgb(60,60,60);\
                            background-color: rgb(1, 147, 195);\
                            padding:6px 6px;\
                            margin:2px 2px;\
                          }";
                 break;
               default:
                 break;
        }
        // 选中样式
        styleMenu += "QMenu::item:selected {\
                             background-color:rgb(235,110,36);\
                      }";

        // 菜单按下效果
        styleMenu += "QMenu::item:pressed {\
                             border: 1px solid rgb(60,60,61);\
                             background-color:rgb(220,80,6);\
                      }";

        fileAction[0] = new QAction(tr("备份"),this);
        fileAction[1]=new QAction(tr("还原"),this);
        fileAction[2]=new QAction(tr("存储状态"),this);
        fileAction[3]=new QAction(tr("文件维护"),this);
        fileAction[4]=new QAction(tr("文件管理"),this);
        fileMenu=new QMenu(tr("文件"));
        fileMenu->addAction(fileAction[0]);
        fileMenu->addAction(fileAction[1]);
        fileMenu->addAction(fileAction[2]);
        fileMenu->addAction(fileAction[3]);
        fileMenu->addAction(fileAction[4]);
        fileMenu->setStyleSheet(styleMenu);//设置样式

        roleAction[0] = new QAction(tr("角色切换"),this);
        roleAction[1] = new QAction(tr("密码修改"),this);
        roleMenu = new QMenu(tr("角色"));
        roleMenu->addAction(roleAction[0]);
        roleMenu->addAction(roleAction[1]);
        roleMenu->setStyleSheet(styleMenu);//设置样式

        runConfigAction[0]=new QAction(tr("坐标系"),this);
        runConfigAction[1]=new QAction(tr("软件使能"),this);
        runConfigAction[2]=new QAction(tr("触摸校准"),this);
        runConfigAction[3]=new QAction(tr("IP设置"),this);
        runConfigAction[4]=new QAction(tr("附加负载"),this);
        runConfigAction[5]=new QAction(tr("安全点"),this);
        runConfigAction[6]=new QAction(tr("干涉区域"),this);
        runConfigAction[7]=new QAction(tr("IO"),this);
        runConfigAction[8]=new QAction(tr("外部启动"),this);//外部启动
        runConfigAction[9]=new QAction(tr("软限位"),this);
        runConfigAction[10]=new QAction(tr("其它"),this);
//        act2[7]=new QAction(tr("以太网"),this);
//        act2[8]=new QAction(tr("RSR1机器人程序"),this);
//        run[2]=new QMenu(tr("IO"));

//        run[2]->addAction(act2[7]);
//        run[2]->addAction(act2[8]);
//        run[2]->addAction(act2[9]);
//        run[2]->addAction(act2[10]);
//        run[2]->addAction(act2[11]);
//        run[2]->addAction(act2[12]);
//        run[2]->addAction(act2[13]);
//        run[2]->addAction(act2[14]);
//        run[1]=new QMenu(tr("启动设置"));
//        run[1]->addMenu(run[2]);
//        run[1]->addAction(act2[6]);

        runConfigMenu = new QMenu(tr("运行配置"));
        runConfigMenu->addAction(runConfigAction[0]);
        runConfigMenu->addAction(runConfigAction[1]);
        runConfigMenu->addAction(runConfigAction[2]);
        runConfigMenu->addAction(runConfigAction[3]);
        runConfigMenu->addAction(runConfigAction[4]);
        runConfigMenu->addAction(runConfigAction[5]);
        //run->addMenu(run[1]);
        runConfigMenu->addAction(runConfigAction[5]);
        runConfigMenu->addAction(runConfigAction[6]);
        runConfigMenu->addAction(runConfigAction[7]);
        runConfigMenu->addAction(runConfigAction[8]);
        runConfigMenu->addAction(runConfigAction[9]);
        runConfigMenu->addAction(runConfigAction[10]);
        runConfigMenu->setStyleSheet(styleMenu);//设置样式

        monitorAction[0] = new QAction(tr("电机数据"),this);
        monitorAction[1] = new QAction(tr("Debug信息"),this);
        monitorAction[2] = new QAction(tr("调度调试信息"),this);
        moniterMenu = new QMenu(tr("监视器"));
        moniterMenu->addAction(monitorAction[0]);
        moniterMenu->addAction(monitorAction[1]);
        moniterMenu->addAction(monitorAction[2]);
        moniterMenu->setStyleSheet(styleMenu);//设置样式

        logAction[0] = new QAction(tr("日志"),this);
        logMenu = new QMenu(tr("日志"));
        logMenu->addAction(logAction[0]);
        logMenu->setStyleSheet(styleMenu);//设置样式

        shutdownAction[0] = new QAction(tr("关机"),this);
//        act5[1] = new QAction(tr("热启动模式"),this);
        shutdownMenu = new QMenu(tr("关机"));
        shutdownMenu->addAction(shutdownAction[0]);
//        shutdown->addAction(act5[1]);
        shutdownMenu->setStyleSheet(styleMenu);//设置样式


        helpAction[0]=new QAction(tr("公司信息"),this);
        helpAction[1]=new QAction(tr("软件信息"),this);
        helpAction[2]=new QAction(tr("注册"),this);
        helpAction[3]=new QAction(tr("固件升级与恢复"),this);
        helpAction[4]=new QAction(tr("升级ＨＭＩ程序"),this);
        //act6[4]=new QAction(tr("HMI"),this);
        //act6[5]=new QAction(tr("控制器"),this);
        //help[1]=new QMenu(tr("升级"));
        //help[1]->addAction(act6[4]);
        //help[1]->addAction(act6[5]);
        helpMenu =new QMenu(tr("帮助"));
        helpMenu->addAction(helpAction[0]);
        helpMenu->addAction(helpAction[1]);
        helpMenu->addAction(helpAction[2]);
        helpMenu->addAction(helpAction[3]);
        helpMenu->addAction(helpAction[4]);
        //help->addMenu(help[1]);
        helpMenu->setStyleSheet(styleMenu);//设置样式

        advanceAction[0]=new QAction(tr("零点标定"),this);
        advanceAction[1]=new QAction(tr("机器人拓扑结构"),this);
        advanceAction[2]=new QAction(tr("EtherCAT设备"),this);
        advanceAction[3]=new QAction(tr("DIDO映射"),this);
        advanceAction[4]=new QAction(tr("机器人参数"),this);
        advanceAction[5]=new QAction(tr("3D仿真配置"),this);
        languageAction[0]=new QAction(tr("中文"),this);
        languageAction[1]=new QAction(tr("English"),this);
        languageMenu = new QMenu(tr("程序编辑语言"));
        languageMenu->addAction(languageAction[0]);
        languageMenu->addAction(languageAction[1]);
        languageMenu->setStyleSheet(styleMenu);
        advanceMenu =new QMenu(tr("高级设置"));
        advanceMenu->addAction(advanceAction[0]);
        advanceMenu->addAction(advanceAction[1]);
        advanceMenu->addAction(advanceAction[2]);
        advanceMenu->addAction(advanceAction[3]);
        advanceMenu->addAction(advanceAction[4]);
        advanceMenu->addAction(advanceAction[5]);
        advanceMenu->addMenu(languageMenu);
        advanceMenu->setStyleSheet(styleMenu);//设置样式


        craftMenu = new QMenu(tr("工艺"));
        action_craft[0]=new QAction(tr("跟踪"),this);
        action_craft[1]=new QAction(tr("焊接属性"),this);
        action_craft[2]=new QAction(tr("焊接条件"),this);
        action_craft[3]=new QAction(tr("摆焊条件"),this);
        action_craft[4]=new QAction(tr("焊接系统"),this);
        action_craft[5]=new QAction(tr("焊缝相机"),this);
        action_craft[6]=new QAction(tr("耦合控制"),this);
        action_craft[7]=new QAction(tr("码跺"),this);
        craftMenu->addAction(action_craft[0]);
        craftMenu->addAction(action_craft[1]);
        craftMenu->addAction(action_craft[2]);
        craftMenu->addAction(action_craft[3]);
        craftMenu->addAction(action_craft[4]);
        craftMenu->addAction(action_craft[5]);
        craftMenu->addAction(action_craft[6]);
        craftMenu->addAction(action_craft[7]);
        craftMenu->setStyleSheet(styleMenu);//设置样式

        setViewText();

        connect(this, SIGNAL(menuSignal(const QPoint&)), this, SLOT(showMenu(const QPoint&)));
        connect(this, SIGNAL(roleSignal(const QPoint&)), this, SLOT(showRole(const QPoint&)));
        connect(this, SIGNAL(runSignal(const QPoint&)), this, SLOT(showRun(const QPoint&)));
        connect(this, SIGNAL(moniterSignal(const QPoint&)), this, SLOT(showMoniter(const QPoint&)));
        connect(this, SIGNAL(logSignal(const QPoint&)), this, SLOT(showLog(const QPoint&)));
        connect(this, SIGNAL(shutdownSignal(const QPoint&)), this, SLOT(showShutdown(const QPoint&)));
        connect(this, SIGNAL(helpSignal(const QPoint&)), this, SLOT(showHelp(const QPoint&)));
        connect(this, SIGNAL(advanceSignal(const QPoint&)), this, SLOT(showAdvance(const QPoint&)));
        connect(manualServonDialog, SIGNAL(setMaxJogTime_signal(int)), this, SIGNAL(setMaxJogTime_signal(int)));
}

void HomeMenuView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
//        ui->checkBox_sevon->setGeometry(600-120,20,90,40);
        ui->pushButton_exitProgram->setGeometry(600-90,450-60,70,40);

        ui->file->setGeometry(10,15,65,60);
        ui->role->setGeometry(10,95,65,60);
        ui->run->setGeometry(10,175,65,60);
        ui->moniter->setGeometry(10,255,65,60);
        ui->log->setGeometry(10,335,65,60);

        ui->shutdown->setGeometry(90,15,65,60);
        ui->help->setGeometry(90,95,65,60);
        ui->advance->setGeometry(90,175,65,60);
        ui->pushButton_agvSetPara->setGeometry(90,255,65,60);
        ui->pushButton_craft->setGeometry(90,335,65,60);
        break;
    default:
        break;
    }
}

HomeMenuView::~HomeMenuView()
{
    delete ui;
}

void HomeMenuView::setViewText()
{
    fileAction[0]->setText(tr("备份"));

    fileAction[1]->setText(tr("还原"));
    fileAction[2]->setText(tr("存储状态"));
    fileAction[3]->setText(tr("文件维护"));
    fileAction[4]->setText(tr("文件管理"));
    fileMenu->setTitle(tr("文件"));

    roleAction[0]->setText(tr("角色切换"));
    roleAction[1]->setText(tr("密码修改"));
    roleMenu->setTitle(tr("角色"));

    runConfigAction[0]->setText(tr("坐标系"));
    runConfigAction[1]->setText(tr("软件使能"));
    runConfigAction[2]->setText(tr("触摸校准"));
    runConfigAction[3]->setText(tr("IP设置"));
    runConfigAction[4]->setText(tr("附加负载"));
    runConfigAction[5]->setText(tr("安全点"));
    runConfigAction[6]->setText(tr("干涉区域"));
    runConfigAction[7]->setText(tr("IO"));
    runConfigAction[8]->setText(tr("外部启动"));//外部启动
    runConfigAction[9]->setText(tr("软限位"));
    runConfigAction[10]->setText(tr("其它"));
//        act2[7]->setText(tr("以太网"));
//        act2[8]->setText(tr("RSR1机器人程序"));
//        run[2]->setTitle(tr("IO"));

//        run[1]->setTitle(tr("启动设置"));

    runConfigMenu->setTitle(tr("运行配置"));

    monitorAction[0]->setText(tr("电机数据"));
    monitorAction[1]->setText(tr("Debug信息"));
    moniterMenu->setTitle(tr("监视器"));

    logAction[0]->setText(tr("日志"));
    logMenu->setTitle(tr("日志"));

    shutdownAction[0]->setText(tr("关机"));
//    act5[1]->setText(tr("热启动模式"));
    shutdownMenu->setTitle(tr("关机"));


    helpAction[0]->setText(tr("公司信息"));
    helpAction[1]->setText(tr("软件信息"));
    helpAction[2]->setText(tr("注册"));
    helpAction[3]->setText(tr("固件升级与恢复"));
    helpAction[4]->setText(tr("升级ＨＭＩ程序"));
    //act6[4]->setText(tr("HMI"));
    //act6[5]->setText(tr("控制器"));
    //help[1]->setTitle(tr("升级"));
    helpMenu->setTitle(tr("帮助"));

    advanceAction[0]->setText(tr("零点标定"));
    advanceAction[1]->setText(tr("机器人拓扑结构"));
    advanceAction[2]->setText(tr("EtherCAT设备"));
    advanceAction[3]->setText(tr("DIDO映射"));
    advanceAction[4]->setText(tr("机器人参数"));
    advanceAction[5]->setText(tr("3D仿真配置"));

    languageAction[0]->setText(tr("中文"));
    languageAction[1]->setText(tr("English"));
    languageMenu->setTitle(tr("程序编辑语言"));

    advanceMenu->setTitle(tr("高级设置"));
}

void HomeMenuView::showMenu(const QPoint &point)
{
    fileMenu->exec(this->mapToGlobal(point));
}

void HomeMenuView::showRole(const QPoint &point1)
{
    roleMenu->exec(this->mapToGlobal(point1));
}

void HomeMenuView::showRun(const QPoint &point2)
{
    runConfigMenu->exec(this->mapToGlobal(point2));
}

void HomeMenuView::showMoniter(const QPoint &point3)
{
    moniterMenu->exec(this->mapToGlobal(point3));
}

void HomeMenuView::showLog(const QPoint &point4)
{
    logMenu->exec(this->mapToGlobal(point4));
}

void HomeMenuView::showShutdown(const QPoint &point5)
{
    shutdownMenu->exec(this->mapToGlobal(point5));
}

void HomeMenuView::showHelp(const QPoint &point6)
{
    helpMenu->exec(this->mapToGlobal(point6));
}

void HomeMenuView::showAdvance(const QPoint &point7)
{
    advanceMenu->exec(this->mapToGlobal(point7));
}

void HomeMenuView::on_file_clicked()
{
    emit menuSignal(menuShowPoint);
}

void HomeMenuView::on_role_clicked()
{
    emit roleSignal(menuShowPoint);
}

void HomeMenuView::on_run_clicked()
{
    emit runSignal(menuShowPoint);
}

void HomeMenuView::on_moniter_clicked()
{
    emit moniterSignal(menuShowPoint);
}

void HomeMenuView::on_log_clicked()
{
    emit logSignal(menuShowPoint);
}

void HomeMenuView::on_shutdown_clicked()
{
    emit shutdownSignal(menuShowPoint);
}

void HomeMenuView::on_help_clicked()
{
    emit helpSignal(menuShowPoint);
}

void HomeMenuView::on_advance_clicked()
{
    emit advanceSignal(menuShowPoint);
}

void HomeMenuView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/menuViewEnglishLan.qm");
        qApp->installTranslator(&translator);

        deviceLogDialog->startCalendar->setLocale(QLocale::English);
        deviceLogDialog->endCalendar->setLocale(QLocale::English);
    }
    else
    {
        qApp->removeTranslator(&translator);

        deviceLogDialog->startCalendar->setLocale(QLocale::Chinese);
        deviceLogDialog->endCalendar->setLocale(QLocale::Chinese);
    }
    ui->retranslateUi(this);
    setViewText();

    companyInfoDialog->changeLanguage();
    copyRightDialog->changeLanguage();
    deviceLogDialog->changeLanguage();
    fileBackUpDialog->changeLanguage();
    fileRestoreDialog->changeLanguage();
    ipSettingDialog->changeLanguage();
    manualServonDialog->changeLanguage();
    motorStatusView->changeLanguage();
    passwordModiyDialog->changeLanguage();
    registrationDialog->changeLanguage();
    restoreDialog->changeLangauge();
    setcoordinatedialog->changeLanguage();
    setZeroPointDialog->changeLanguage();
    startTypeDialog->changeLanguage();
    touchCheckDialog->changeLanguage();
}

void HomeMenuView::showLoginView()
{
    loginDialog->show();
}





void HomeMenuView::on_pushButton_exitProgram_clicked()
{
//    if (!(QMessageBox::information(this,tr("exit tip"),tr("是否退出本程序?"),tr("是"),tr("否"))))

    QMessageBox message(QMessageBox::NoIcon, "提示", "是否退出本程序?"
                        , QMessageBox::Yes | QMessageBox::No, NULL);
    if(message.exec() == QMessageBox::Yes)
    {
//       QApplication* app; app->exit(0);
       QApplication::exit( 0 );
    }

}

void HomeMenuView::on_pushButton_agvSetPara_clicked()
{
    emit showAgvSetParaDiagSignal();
}

void HomeMenuView::on_pushButton_craft_clicked()
{
    craftMenu->exec(this->mapToGlobal(menuShowPoint));
}
